<template>
  <div class="app-container">
    <div class="demo-input-suffix">
      <el-input
          placeholder="商品标题、商品类型"
          prefix-icon="el-icon-search"
          v-model="page_param.key"
          @input="getQueryList"
          style="width: 300px;margin: 5px"
      >
      </el-input>
    </div>

    <!--    <el-menu :default-active="page_param.status" class="el-menu-demo" mode="horizontal" @select="handleSelect">-->
    <!--      <el-menu-item index="">全部</el-menu-item>-->
    <!--      <el-menu-item index="1">待审核</el-menu-item>-->
    <!--      <el-menu-item index="2">审核失败</el-menu-item>-->
    <!--      <el-menu-item index="9">上线</el-menu-item>-->
    <!--      <el-menu-item index="12">售出</el-menu-item>-->
    <!--    </el-menu>-->
    <el-row :gutter="10" class="mb8" style="margin-bottom: 7px">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--            type="primary"-->
<!--            plain-->
<!--            icon="el-icon-plus"-->
<!--            size="mini"-->
<!--            @click="handleAdd"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-delete"
            size="mini"
            @click="handleEdit"
        >编辑
        </el-button>
      </el-col>

    </el-row>
    <el-table v-loading="loading" :data="infoData.records" element-loading-text="Loading" border fit
              highlight-current-row
              @selection-change="handleSelectionChange"
    >

      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column align="center" label="图片">
        <template v-slot="scope">
          <el-image style="height: 100px;width: 100px;border-radius: 10px" :src="scope.row.image"></el-image>
        </template>
      </el-table-column>

      <el-table-column align="center" label="标题">
        <template v-slot="scope">
          {{ scope.row.title }}
        </template>
      </el-table-column>

      <el-table-column align="center" label="类型">
        <template v-slot="scope">
          {{ scope.row.typeName }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="商品价格">
        <template v-slot="scope">
          {{ scope.row.price }} 元
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template v-slot="scope">
          {{ statusMap[scope.row.status] || scope.row.status }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template v-slot="scope">
          {{ $utils.convert.parseTime(scope.row.createTime) }}
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template v-slot="scope">
          <el-button
              size="mini"
              type="primary"
              @click="toDetail(scope.row)">查看详情
          </el-button>
          <br v-if="scope.row.status === 9">
          <el-button
              v-if="scope.row.status === 9"
              size="mini"
              type="warning"
              style="margin-top: 10px"
              @click="down(scope.row)">下架商品
          </el-button>
          <div v-if="scope.row.status === 1">
            <el-button
                style="margin-top: 10px"
                v-if="scope.row.status === 1"
                size="mini"
                type="success"
                @click="pass(scope.row)">审核通过
            </el-button>
            <br>
            <el-button
                style="margin-top: 10px"
                v-if="scope.row.status === 1"
                size="mini"
                type="danger"
                @click="fail(scope.row)">审核失败
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--    <el-pagination style="display: flex;justify-content: center;margin-top: 10px"
                       layout="total,prev, pager, next,jumper" :total="infoData.total"
                       :current-page.sync="infoData.current"
                       @current-change="handleCurrentChange">
        </el-pagination>
        <el-dialog title="添加优惠卷" :visible.sync="dialog">
          <el-form :model="formData">

            <el-form-item label="折扣金额" label-width="120px">
              <el-input-number required controls-position="right" v-model="formData.voucherValue" :precision="2" :step="1"
                               :min="0"></el-input-number>
            </el-form-item>
            <el-form-item label="抢购时间" label-width="120px">
              <el-date-picker
                  v-model="formData.time"
                  type="datetimerange"
                  range-separator="至"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="postData">确 定</el-button>
          </div>
        </el-dialog>-->

    <!--添加修改弹出层-->
    <el-dialog :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上传图片" prop="">
              <el-upload
                  v-model:file-list="fileList"
                  action="http://127.0.0.1:5050/upload/image"
                  list-type="picture-card"
                  :on-success="handleUploadSuccess"
              >
                <div style="line-height: 80px;font-size: 12px;color: #9999b3">+添加优质图</div>
              </el-upload>
              <el-dialog :visible.sync="dialog.visible">
                <img width="100%" :src="dialog.imageUrl" alt="预览图像">
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品标题" prop="">
              <el-input
                  v-model="submitData.title"
                  placeholder="请输入商品标题"
                  maxlength="11"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="详细信息" prop="email">
              <el-input
                  v-model="submitData.intro"
                  placeholder="请输入商品详细信息"
                  :rows="5"
                  maxlength="200"
                  type="textarea"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="商品分类" prop="type" style="width:200px ">
          <el-select v-model="submitData.typeName" placeholder="选择商品分类">
            <el-option v-for="(item,index) in menuList" :key="item.id" :label="item.typeName"
                       :value="item.typeName"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="发货方式" prop="postType">
          <el-radio-group v-model="submitData.postType" required>
            <el-radio :label="0">邮寄</el-radio>
            <el-radio :label="1">自提</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="价格">
          <el-input-number required controls-position="right" v-model="submitData.price" :precision="2" :step="1"
                           :min="0"></el-input-number>
        </el-form-item>
        <el-form-item label="原价">
          <el-input-number required controls-position="right" v-model="submitData.originalPrice" :precision="2"
                           :step="1" :min="0"></el-input-number>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      open: false,
      // 表单参数
      form: {},
      page_param: {
        key: "",
        pageNumber: 1,
        pageSize: 10,
        status: '',
      },
      formData: {
        productId: '',
        voucherValue: 0,
        beginTime: '',
        endTime: '',
        time: '',
      },
      dialog: false,
      infoData: {},
      loading: true,

      //提交信息
      fileList: [],
      menuList: [],
      // 多选框选中数组
      ids: [],
      submitData: {
        title: '',
        intro: '',// 商品详情
        price: '',
        image: '',
        originalPrice: '',
        postType: '1',
        typeName: '',
        typeCode: '',
        id: '',
        createTime: '',
      }
    }

  },
  created() {
    this.getProductInfo()
  },
  computed: {
    statusMap() {
      return {
        1: '待审核',
        8: '订单完成',
        9: '已上架',
        2: '审核失败',
        10: '商品下架',
      };
    }
  },
  methods: {
    getProductInfo() {
      this.$api.productInfo.getProductInfoPage(this.page_param).then(res => {
        this.infoData = res.result
        this.loading = false
        this.infoData.records.forEach(item => {
          if (item.image) {
            item.image = JSON.parse(item.image)[0]
          }
        })
      })
    },
    postData() {
      let data = {...this.formData}
      let start = new Date(data.time[0]);
      data.beginTime = Math.floor(start.getTime() / 1000);
      let end = new Date(data.time[1]);
      data.endTime = Math.floor(end.getTime() / 1000);
      this.$api.productInfo.addVoucher(data).then(res => {
        this.$message({type: 'success', message: '操作成功!'});
        this.dialog = false
        this.getProductInfo()
      })
    },
    pass(item) {
      this.$confirm('确认审核通过?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.productInfo.passProduct(item.id).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getProductInfo()
        })
      })
    },
    down(item) {
      this.$confirm('确认下架?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.productInfo.downProduct(item.id).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getProductInfo()
        })
      })
    },
    fail(item) {
      this.$confirm('确认审核失败?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.productInfo.failProduct(item.id).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getProductInfo()
        })
      })
    },
    handleCurrentChange(val) {
      this.page_param.pageNumber = val
      this.getProductInfo()
    },
    getQueryList(val) {
      this.page_param.key = val
      this.getProductInfo()
    },
    toDetail(row) {
      this.$router.push('/product/product_detail?id=' + row.id)
    },
/*    handlePreview(file) {
      this.dialog.imageUrl = file.url;
      this.dialog.visible = true;
    },*/
    handleRemove(file, fileList) {
      this.$api.image.removeImage(file.response.result.name).then(res => {
        this.fileList = fileList
      })
    },
    handleUploadSuccess(response, file, fileList) {
      this.fileList = fileList
    },
    handleAdd() {
      this.reset()
      this.getMenuList()
      this.open = true;

    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$confirm('确认进行删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'danger'
      }).then(() => {
        this.$api.productInfo.deleteProductInfo(ids).then(res => {
          this.$message({
            type: 'success',
            message: '操作成功!'
          });
          this.getProductInfo()
        })
      })

    },
    handleEdit(row) {
      this.getMenuList();
      this.open = true;
      const id = row.id || this.ids
      if (id.length > 1) {
        this.$message({type: 'warning', message: '不能同时编辑多个选项!'});
        this.ids = undefined
      }
      this.$api.productInfo.getProductInfoDetail(id).then(res => {
        console.log(res)
        this.submitData.id = res.result.productInfo.id
        this.submitData.title = res.result.productInfo.title
        this.submitData.image = res.result.productInfo.image
        this.submitData.intro = res.result.productInfo.intro
        this.submitData.typeName = res.result.productInfo.typeName
        this.submitData.typeCode = res.result.productInfo.typeCode
        this.submitData.postType = res.result.productInfo.postType
        this.submitData.originalPrice = res.result.productInfo.originalPrice
        this.submitData.price = res.result.productInfo.price
      })
    },
    //表单重置
    reset() {
      this.submitData.id = undefined
      this.submitData.image = ''
      this.submitData.title = undefined
      this.submitData.intro = undefined
      this.submitData.typeName = undefined
      this.submitData.typeCode = undefined
      this.submitData.postType = undefined
      this.submitData.originalPrice = undefined
      this.submitData.price = undefined
    },

    cancel() {
      this.open = false
      this.reset()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    getMenuList() {
      this.$api.productType.getTypeList().then(res => {
        this.menuList = res.result

      })
    },
    submitForm() {
      //图片处理
      if (this.fileList.length > 0) {
        let _fileList = this.fileList.map(file => file.response.result.url);
        this.submitData.image = JSON.stringify(_fileList)
      }
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.submitData.id !== undefined) {
            this.$api.productInfo.editProductInfo(this.submitData).then(res => {
              this.open = false
              this.$message({type: 'success', message: '修改成功!'});
              this.getProductInfo()

            })
          } else {
            this.$api.productInfo.createProductInfo(this.submitData).then(res => {
              this.open = false
              this.$message({type: 'success', message: '添加成功!'});
              this.getProductInfo()
            })
          }
          this.reset()
        }
      })
    },
  }
}

</script>

